/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import * as path from 'path';

import { initByStageEmptyApp, initByStageEmptyService, initExecuteCommand, runHvigor } from './common/common';
import { ModuleBuildProfileModifier } from './common/modifier/module-build-profile-modifier';

describe('PackageApp test', () => {
  test.concurrent('test stage app package app', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'project',
        product: 'default',
        tasks: 'PackageApp'
      });
      const result = runHvigor(projectRoot, commands);
      expect(result.stdout.includes('Finished ::PackageApp')).toBeTruthy();
      expect(result.stdout.includes('project has submodules:entry,feature,library,library1')).toBeTruthy();
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const result2 = runHvigor(projectRoot, commands);
      expect(result2.stdout.includes('UP-TO-DATE ::PackageApp')).toBeTruthy();
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const output = path.resolve(projectRoot, 'build/outputs');
      fse.removeSync(output);
      const result3 = runHvigor(projectRoot, commands);
      expect(result3.stdout.includes('Finished ::PackageApp')).toBeTruthy();
      expect(result.stdout.includes('project has submodules:entry,feature,library,library1')).toBeTruthy();
      expect(result3.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
    });
  });

  test.concurrent('test stage service package app', async () => {
    await initByStageEmptyService(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'project',
        product: 'default',
        tasks: 'PackageApp'
      });

      new ModuleBuildProfileModifier(projectRoot, 'entry').updateTargetRuntimeOS('default', 'HarmonyOS');
      new ModuleBuildProfileModifier(projectRoot, 'feature').updateTargetRuntimeOS('default', 'HarmonyOS');
      const modifyRuntimeOSResult = runHvigor(projectRoot, commands);
      expect(modifyRuntimeOSResult.stdout.includes('Finished ::PackageApp')).toBeTruthy();
      expect(modifyRuntimeOSResult.stdout.includes('project has submodules:entry,feature,library,library1'))
        .toBeTruthy();
      expect(modifyRuntimeOSResult.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const result2 = runHvigor(projectRoot, commands);
      expect(result2.stdout.includes('UP-TO-DATE ::PackageApp')).toBeTruthy();
      expect(modifyRuntimeOSResult.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const output = path.resolve(projectRoot, 'build/outputs');
      fse.removeSync(output);
      const result3 = runHvigor(projectRoot, commands);
      expect(result3.stdout.includes('Finished ::PackageApp')).toBeTruthy();
      expect(modifyRuntimeOSResult.stdout.includes('project has submodules:entry,feature,library,library1'))
        .toBeTruthy();
      expect(result3.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
    }, { sign: false });
  });
});
